<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Loader;
use think\exception\HttpException;
use think\Config;
use think\Image;
use think\Request;

class Casee extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];


    public function add()
    {
    	$controller = $this->request->controller();

        if ($this->request->isAjax()) {
            // 插入
            $data = $this->request->except(['id']);

            // 验证
            if (class_exists($validateClass = Loader::parseClass(Config::get('app.validate_path'), 'validate', $controller))) {
                $validate = new $validateClass();
                if (!$validate->check($data)) {
                    return ajax_return_adv_error($validate->getError());
                }
            }

            // 写入数据
            if (
                class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $this->parseCamelCase($controller)))
                || class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $controller))
            ) {
                //使用模型写入，可以在模型中定义更高级的操作
                $model = new $modelClass();
                $ret = $model->isUpdate(false)->save($data);
            } else {
                // 简单的直接使用db写入
                Db::startTrans();
                try {
                    $model = Db::name($this->parseTable($controller));
                    $ret = $model->insert($data);
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();

                    return ajax_return_adv_error($e->getMessage());
                }
            }

            return ajax_return_adv('添加成功');
        } else {
            // 添加
            return $this->view->fetch();
        }
    }


    public function content()
    {
        $id = input('id');
        
        $data = Db::name('casee')->where('id',$id)->find();
        $type = $data['type'];
        $this->view->assign('type',$type);
        $data = htmlspecialchars_decode($data['content']);
      
        $server = Db::name('server')->select();

        $this->view->assign('data',$data);
        
        $this->view->assign('server',$server);
        $this->view->assign('id',$id);
        return $this->view->fetch();
    }

    /**
    * 图片上传
    */
    public function uploads(Request $request)
    {
        $file = $request->file('wangEditorH5File');

        $time = date('Ymd');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'static'.DS.'yanxue'.DS.$time,'');

        if($info){
            echo 'http://www.nashengcm.com/static/yanxue/'.$time.'/'.$info->getSaveName();
        }
    }

    //上传内容保存
    public function editcasee()
    {   
        $id = input('post.id');
        $content = input('post.content');
        $time = time();

        $data['content'] = $content;
        $data['update_time'] = time();
        $data['link'] = url('index/index/caseeshow').'?id='.$id;
        $data['type'] = input('post.type');
        
        $res = Db::name('casee')->where('id',$id)->update($data);

        if($res){
        	echo '200';
        }else{
        	echo '201';
        }
    }
    
}
